/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useMemo } from 'react';
import ProTable from '@ant-design/pro-table';
import DragTable from '@/components/DragTable';
import { Input } from 'antd';
import { useSelector, useDispatch } from 'umi';
import { getInfoVerifyList } from '@/services/operatorManagement';
import type { ProColumns } from '@ant-design/pro-table';
import type { ConnectState } from '@/models/connect';

type PropsType = {
  type: 1 | 2 | 3 | number;
  activeKey: 1 | 2 | 3 | number;
  checkDetail: (id: string) => void;
};

/**
 * 经营信息审核列表
 *  */
const Index: React.FC<PropsType> = ({ type, checkDetail }) => {
  const dispatch = useDispatch();
  const { pagination = {} } = useSelector((state: ConnectState) => state.yzInfoVerify);

  const columns = useMemo(() => {
    const list: ProColumns<YzInfoVerify.Item>[] = [
      {
        dataIndex: 'filter',
        hideInTable: true,
        hideInSearch: type != 3,
        renderFormItem: () => {
          return <Input placeholder='输入账号/驿站名称查询' />;
        },
      },
      {
        title: '账号',
        width: 150,
        dataIndex: 'phone',
        search: false,
        align: 'center',
      },
      {
        title: '驿站名称',
        width: 300,
        dataIndex: 'inn_name',
        search: false,
        align: 'center',
      },
      {
        title: '所属加盟商',
        width: 300,
        dataIndex: 'league_name',
        search: false,
        align: 'center',
      },
      {
        title: '提交时间',
        width: 300,
        dataIndex: 'update_time',
        search: false,
        align: 'center',
      },
      {
        title: '操作人',
        width: 300,
        dataIndex: 'operator',
        search: false,
        align: 'center',
      },
      {
        title: '操作',
        width: 300,
        align: 'center',
        valueType: 'option',
        renderText(_, record) {
          return <a onClick={() => checkDetail(record.id)}>查看</a>;
        },
      },
    ];
    return list.filter((val) => (type == 1 ? val.dataIndex != 'operator' : val));
  }, [type]);

  const requestTableData = async (values: any) => {
    const {
      current: page,
      pageSize: limit,
      filter,
    }: { current: number; pageSize: number; filter?: string } = values;
    const data = await getInfoVerifyList({
      page,
      limit,
      type,
      filter,
    });
    return data;
  };

  return (
    <DragTable>
      <ProTable<YzInfoVerify.Item>
        rowKey='id'
        columns={columns}
        options={false}
        search={type == 3 ? undefined : false}
        request={requestTableData}
        pagination={{
          current: pagination[type]?.page,
          pageSize: pagination[type]?.pageSize || 10,
          onChange(page, pageSize) {
            dispatch({
              type: 'yzInfoVerify/save',
              payload: {
                pagination: {
                  ...pagination,
                  [type]: { page, pageSize },
                },
              },
            });
          },
        }}
      />
    </DragTable>
  );
};

export default Index;
